﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LoginServer.Converter
{
    public class ConvertDataMachine
    {
        public ConvertDataMachine()
        {

        }
        public object[] GetData(object[] param)
        {
            object[] tmp = new object[param.Length - 1];
            for (int i = 1; i < param.Length;i++ )
            {
                tmp[i - 1] = param[i];
            }
            return tmp;
        }
        public MachineConnection GetMachine(object[] param)
        {
            return (MachineConnection)param[0];
        }
        public void ConvertReceive_L_CreateTableOK(ref int UserCreate,ref int IDTable, ref List<byte> IPandPort,object[] param)
        {
            object[] data = GetData(param);
            byte[] tmp = LConverter.ConvertObjectToByte(data);
            byte[] tmpUserID = new byte[4];
            byte[] tmpTableID = new byte[4];            
            for (int i = 0; i < 4; i++)
            {
                tmpUserID[i] = tmp[i];
            }
            for (int i = 4; i < 8; i++)
            {
                tmpTableID[i - 4] = tmp[i];
            }            
            for (int i = 8; i < tmp.Length; i++)
            {
                IPandPort.Add(tmp[i]);
            }
            UserCreate= BitConverter.ToInt32(tmpUserID, 0);
            IDTable = BitConverter.ToInt32(tmpTableID, 0);
        }
        public void ConvertReceive_L_JoinTableOK(ref int UserJoin, ref int TableIDJoin, ref List<byte> IPandPort, object[] param)
        {
            object[] data = GetData(param);
            byte[] tmp = LConverter.ConvertObjectToByte(data);
            byte[] tmpUserID = new byte[4];
            byte[] tmpTableID = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                tmpUserID[i] = tmp[i];
            }
            for (int i = 4; i < 8; i++)
            {
                tmpTableID[i - 4] = tmp[i];
            }
            for (int i = 8; i < tmp.Length; i++)
            {
                IPandPort.Add(tmp[i]);
            }
            UserJoin = BitConverter.ToInt32(tmpUserID, 0);
            TableIDJoin = BitConverter.ToInt32(tmpTableID, 0);
        }


        internal void ConvertReceive_L_CreateRoomOK(ref int UserID, ref int RoomID, ref string RoomName, object[] param)
        {
            object[] data = GetData(param);
            byte[] tmp = LConverter.ConvertObjectToByte(data);
            UserID = BitConverter.ToInt32(tmp, 0);
            RoomID = BitConverter.ToInt32(tmp, 4);
            string[] r = Encoding.UTF8.GetString(tmp, 0, tmp.Length).Substring(12).Split(":".ToCharArray());
            RoomName = r[0];
        }

        internal void ConvertReceive_L_JoinRoomOK(ref int UserJoinID, ref int RoomJoinID, object[] param)
        {
            object[] data = GetData(param);
            byte[] tmp = LConverter.ConvertObjectToByte(data);
            UserJoinID = BitConverter.ToInt32(tmp, 0);
            RoomJoinID = BitConverter.ToInt32(tmp, 4);
        }
    }
}
